﻿//#define USE_PIXELVIEWER

using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace HexPixel.Forms
{
	public partial class PixelViewForm : Form
	{
		Bitmap bmpPixelImage = null;

		public PixelViewForm()
		{
			InitializeComponent();

#if USE_PIXELVIEWER
			this.pixelViewer.HorizontalScroll.SmallChange = (int)(this.pixelViewer.AutoScrollMinSize.Width / 20);
			this.pixelViewer.HorizontalScroll.LargeChange = (int)(this.pixelViewer.AutoScrollMinSize.Width / 10);
			this.pixelViewer.VerticalScroll.SmallChange = (int)(this.pixelViewer.AutoScrollMinSize.Height / 20);
			this.pixelViewer.VerticalScroll.LargeChange = (int)(this.pixelViewer.AutoScrollMinSize.Height / 10);
			this.pixelViewer.Visible = true;
#else
			this.pictureBox.Visible = true;
#endif

			this.ResumeLayout(false);
		}

		private void CreateBitmap(Color[] pixelData, Size imageSize, PixelSize pixelSize)
		{
			if (pixelData == null)
				return;

			if (pixelSize == PixelSize.BIT_24)
			{
				this.bmpPixelImage = new Bitmap(imageSize.Width, imageSize.Height, PixelFormat.Format24bppRgb);
			}
			else if (pixelSize == PixelSize.BIT_32)
			{
				this.bmpPixelImage = new Bitmap(imageSize.Width, imageSize.Height, PixelFormat.Format32bppRgb);
			}

			for (int pos = 0; pos < pixelData.Length; ++pos)
			{
				int nPosX = pos % imageSize.Width;
				int nPosY = pos / imageSize.Width;

				this.bmpPixelImage.SetPixel(nPosX, nPosY, pixelData[pos]);
			}
		}

		public void SetPixelData(Color[] arrColorData, int nWidth, PixelSize pixelSize)
		{
			int imageWidth = nWidth;
			int imageHeight = arrColorData.Length / nWidth;
			if (arrColorData.Length % nWidth > 0)
				imageHeight += 1;

			CreateBitmap(arrColorData, new Size(imageWidth, imageHeight), pixelSize);

#if USE_PIXELVIEWER
			this.pixelViewer.Image = bmpPixelImage;
#else
			this.pictureBox.Image = bmpPixelImage;
#endif
		}
	}
}
